<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>根据指定格式解析输入的字符</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.sprintf.html">sprintf</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.str-getcsv.html">str_getcsv</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.sscanf" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sscanf</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5)</p><p class="refpurpose"><span class="refname">sscanf</span> &mdash; <span class="dc-title">根据指定格式解析输入的字符</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.sscanf-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>sscanf</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter reference">&$...</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   这个函数 <span class="function"><strong>sscanf()</strong></span> 输入类似
   <span class="function"><a href="function.printf.html" class="function">printf()</a></span>. <span class="function"><strong>sscanf()</strong></span>
   读取字符串<em><code class="parameter">str</code></em> 然后根据指定格式<em><code class="parameter">format</code></em>解析, 格式的描述文档见 <span class="function"><a href="function.sprintf.html" class="function">sprintf()</a></span>. 
  </p>
  <p class="para">
   指定的格式字符串中的任意空白匹配输入字符串的任意空白.也就是说即使是格式字符串中的一个制表符 \t 也能匹配输入
   字符串中的一个单一空格字符
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.sscanf-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">str</code></em></dt>

     <dd>

      <p class="para">
       将要被解析的 <span class="type"><span class="type 字符串">字符串</span></span>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">format</code></em></dt>

     <dd>

      <p class="para">
       The interpreted format for 解析<em><code class="parameter">str</code></em>的格式, 除了以下不同外，其余的见
       <span class="function"><a href="function.sprintf.html" class="function">sprintf()</a></span>的描述文档:
       <ul class="simplelist">
        <li class="member">
         函数不区分语言地区
        </li>
        <li class="member">
         <em>F</em>, <em>g</em>, <em>G</em> 和
         <em>b</em> 不被支持.
        </li>
        <li class="member">
         <em>D</em> 表示十进制数字.
        </li>
        <li class="member">
         <em>i</em> stands for integer with base detection.
        </li>
        <li class="member">
         <em>n</em> stands for number of characters processed so far.
        </li>
       </ul>
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">...</code></em></dt>

     <dd>

      <p class="para">
       可以选参数将以引用方式传入，它们的值将被设置为解析匹配的值
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.sscanf-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果仅传入了两个参数给这个函数，解析后将返回一个数组，否则，如果可选参数被传入，这个函数将返回被设置了值的个数
  </p>
  <p class="para">
   如果<em><code class="parameter">format</code></em>存在的子字符串比
   <em><code class="parameter">str</code></em>内可用的多,
   <em>-1</em> 将被返回.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.sscanf-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4775">
    <p><strong>Example #1 <span class="function"><strong>sscanf()</strong></span> Example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;getting&nbsp;the&nbsp;serial&nbsp;number<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$serial</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">sscanf</span><span style="color: #007700">(</span><span style="color: #DD0000">"SN/2350001"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SN/%d"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;and&nbsp;the&nbsp;date&nbsp;of&nbsp;manufacturing<br /></span><span style="color: #0000BB">$mandate&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"January&nbsp;01&nbsp;2000"</span><span style="color: #007700">;<br />list(</span><span style="color: #0000BB">$month</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$day</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">sscanf</span><span style="color: #007700">(</span><span style="color: #0000BB">$mandate</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"%s&nbsp;%d&nbsp;%d"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Item&nbsp;</span><span style="color: #0000BB">$serial</span><span style="color: #DD0000">&nbsp;was&nbsp;manufactured&nbsp;on:&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #DD0000">-"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$month</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"-</span><span style="color: #0000BB">$day</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   If optional parameters are passed, the function will return the
   number of assigned values.
  </p>
  <p class="para">
   <div class="example" id="example-4776">
    <p><strong>Example #2 <span class="function"><strong>sscanf()</strong></span> - using optional parameters</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;get&nbsp;author&nbsp;info&nbsp;and&nbsp;generate&nbsp;DocBook&nbsp;entry<br /></span><span style="color: #0000BB">$auth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"24\tLewis&nbsp;Carroll"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sscanf</span><span style="color: #007700">(</span><span style="color: #0000BB">$auth</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"%d\t%s&nbsp;%s"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$first</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$last</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"&lt;author&nbsp;id='</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">'&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;firstname&gt;</span><span style="color: #0000BB">$first</span><span style="color: #DD0000">&lt;/firstname&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;surname&gt;</span><span style="color: #0000BB">$last</span><span style="color: #DD0000">&lt;/surname&gt;<br />&lt;/author&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.sscanf-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.fscanf.html" class="function" rel="rdfs-seeAlso">fscanf()</a> - 从文件中格式化输入</span></li>
    <li class="member"><span class="function"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - 输出格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - Return a formatted string</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.sprintf.html">sprintf</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.str-getcsv.html">str_getcsv</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
